

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.common.exception.RRException;
import com.yz.common.utils.R;
import com.yz.common.validator.Assert;
import com.yz.dao.ProjectDao;
import com.yz.dao.UserDao;
import com.yz.form.LoginForm;
import com.yz.service.*;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("projectService")
public class ProjectServiceImpl extends ServiceImpl<ProjectDao, ProjectEntity> implements ProjectService {
    @Autowired
    private ProjectAboutService projectAboutService;
    @Autowired
    private ProjectUserService projectUserService;
    @Autowired
    private UserService userService;
    @Autowired
    RuinNameService ruinNameService;


    @Override
    @Transactional(rollbackFor=Exception.class)
    public List<ProjectForm> getUnfinishProject(String keyWord,UserEntity userEntity) {
        if(keyWord==null){
            keyWord="";
        }
        List<Long> projectAboutEntities =projectUserService.selectProjectId(userEntity.getUserId());
        List<ProjectEntity> projectEntitys = new ArrayList<ProjectEntity>();
        List<ProjectForm> projectForms = new ArrayList<ProjectForm>();
        String group = "update_at desc,id desc";
        ProjectEntity projectEntity = new ProjectEntity();
        for (int i = 0; i < projectAboutEntities.size(); i++) {
            projectEntity = this.selectOne(new EntityWrapper<ProjectEntity>().eq("id",projectAboutEntities.get(i)).eq("status",1).where("schedule!=6"+" and (number like '%"+keyWord+"%' or name like '%"+keyWord+"%' or unit like '%"+keyWord+"%')").groupBy(group));
            if(projectEntity!=null){
                projectEntitys.add(projectEntity);
            }
        }
        for (int i = 0; i < projectEntitys.size(); i++) {
            projectEntity = projectEntitys.get(i);
            StringBuffer agreemen = new StringBuffer();
            StringBuffer response = new StringBuffer();
            StringBuffer projectMan = new StringBuffer();
            List<Integer> userIds = new ArrayList<Integer>();
            List<ProjectUserEntity> users = projectUserService.selectList(new EntityWrapper<ProjectUserEntity>().eq("project_id",projectEntitys.get(i).getId()));
            ProjectForm item = new ProjectForm();
            for (int j = 0; j < users.size(); j++) {
                if(users.get(j).getType()==3) {//项目协议人
                    if (agreemen.toString().isEmpty()) {
                        agreemen.append("" + userService.selectById(users.get(j).getUserId()).getUsername());
                    } else {
                        agreemen.append("," + userService.selectById(users.get(j).getUserId()).getUsername());
                    }
                }
//                if(users.get(j).getType()==1) {//项目负责人
//                    if (response.toString().isEmpty()) {
//                        response.append("" + userService.selectById(users.get(j).getUserId()).getUsername());
//                    } else {
//                        response.append("," + userService.selectById(users.get(j).getUserId()).getUsername());
//                    }
//                }
                if(users.get(j).getType()==2){//项目成员
                    if(projectMan.toString().isEmpty()){
                        projectMan.append(""+userService.selectById(users.get(j).getUserId()).getUsername());
                    }else{
                        projectMan.append(","+userService.selectById(users.get(j).getUserId()).getUsername());
                    }
                }
            }
            StringBuffer ruin = new StringBuffer();
            List<RuinNameEntity> ruins = ruinNameService.selectList(new EntityWrapper<RuinNameEntity>().eq("project_id",projectEntitys.get(i).getId()));
            for (int j = 0; j < ruins.size(); j++) {
                if (j==0){
                    ruin.append(""+ruins.get(j).getRelicName());
                }else {
                    ruin.append(","+ruins.get(j).getRelicName());
                }
            }
            item.setAgreementMan(agreemen.toString());
            item.setProjectReponseMan(projectEntity.getHeaderName());
            item.setProjectMan(projectMan.toString());
            item.setAddress(projectEntity.getAddress());
            item.setCreateAt(projectEntity.getCreateAt());
            item.setEndDate(projectEntity.getEndDate());
            item.setCreateUser(projectEntity.getCreateUser());
            item.setId(projectEntity.getId());
            item.setName(projectEntity.getName());
            item.setNumber(projectEntity.getNumber());
            item.setPeriod(projectEntity.getPeriod());
            item.setSchedule(projectEntity.getSchedule());
            item.setStartDate(projectEntity.getStartDate());
            item.setStatus(projectEntity.getStatus());
            item.setUnit(projectEntity.getUnit());
            item.setUpdateAt(projectEntity.getUpdateAt());
            item.setUserid(projectEntity.getUserid());
            item.setRuinName(ruin.toString());
            projectForms.add(item);
        }
        return projectForms;
    }
    }
